package tacos.messing.rabbitmq;

import org.springframework.amqp.support.converter.DefaultClassMapper;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import tacos.domain.Order;

import java.util.HashMap;
import java.util.Map;

@Profile("rabbit-template")
@Configuration
public class MessagingConfig {

    @Bean
    public MessageConverter messageConverter(){
        // 用于替换默认的SimpleMessageConverter
        Jackson2JsonMessageConverter converter = new Jackson2JsonMessageConverter();
        converter.setClassMapper(classMapper());
        return converter;
    }

    @Bean
    public DefaultClassMapper classMapper() {
        DefaultClassMapper classMapper = new DefaultClassMapper();
        Map<String, Class<?>> idClassMapping = new HashMap<>();
        idClassMapping.put("order", Order.class);
        classMapper.setIdClassMapping(idClassMapping);
        return classMapper;
    }
}
